package com.goldsign.pay.provider.sample.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
//import org.springframework.cloud.client.ServiceInstance;
//import org.springframework.cloud.client.discovery.DiscoveryClient;
//import org.springframework.cloud.client.serviceregistry.Registration;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.goldsign.pay.feign.api.sample.entity.Sample;
import com.goldsign.pay.provider.sample.biz.SampleBiz;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.core.Application;


/**
 * 
 * 1、使用@RefreshScope注解的类，会在接到SpringCloud配置中心配置刷新的时候，自动将新的配置更新到该类对应的字段中
 * 
 * 2、在采用RESTful的时候，严格按照约定来使用 HTTP METHOD：【GET-查询 ，POST-创建， PUT-修改，DELETE-删除 】,
 * 由于系统使用逻辑删除，所以更新、删除操作使用POST方法
 * 
 * 3、Content-Type= application/json
 * 	   在GET请求中，不能使用@RequestBody。
 * 	   在POST请求， 可以使用@RequestBody 和 @RequestParam，但是如果使用@RequestBody，对于参数转化的配置必须统一
 */

/**
 * samplecode
 * @author johnny
 *
 */
@RefreshScope 
@RestController
@RequestMapping(value = "sample",produces={MediaType.APPLICATION_JSON_VALUE})
@Api(value = "sampleApi", description="样例服务API",tags={"sample"})
public class SampleController {

    private static final Logger LOGGER = LoggerFactory.getLogger(SampleController.class);

    @Value("${info.name}")
    private String name;
    
//    @Autowired
//    private Registration registration;       // 服务注册
//
//    @Autowired
//    private DiscoveryClient discoveryClient; // 服务发现客户端
    
    @Autowired
    private SampleBiz sampleBiz;
    
    
    /**
     * 查询列表
     * @param id
     * @return
     */
    @ApiOperation(value="样例-列表",notes="根据url的name来获取列表",produces = MediaType.APPLICATION_JSON_VALUE )
//    @ApiImplicitParams(value={@ApiImplicitParam(name="name",value="用户名",dataType="String", paramType = "query")})
//    @ApiImplicitParam(name = "name", value = "用户name", required = true, dataType = "String", paramType = "query")
    @RequestMapping(method={RequestMethod.GET}  ,value = "/l/{name}")
    @ResponseBody
    public List<Sample> list(@PathVariable  String name) {
    	LOGGER.info("list name :{}" , name);
        Sample entity = new Sample();entity.setSamplename(name);
        List<Sample> ls = sampleBiz.findList(entity);
        return ls;
    }
   
    /**
     * 创建/修改
     * @param code
     * @return
     */
    @ApiOperation(value="样例-保存",notes="POST对象",produces = MediaType.APPLICATION_JSON_VALUE )
    @RequestMapping(method={RequestMethod.POST} ,value = "/s")
    @ResponseBody
    public Sample save(@RequestBody Sample entity) {
    	LOGGER.info("save Sample:{}" , entity.toString());
    	int cnt = sampleBiz.save(entity);
    	LOGGER.info("save cnt:{}" , cnt);
    	return entity;
    }
    
    /**
     * 删除（@RequestParam绑定参数）
     * @param id
     * @return
     */
    @RequestMapping(method={RequestMethod.POST}  ,value = "/d")
    /** 
     * 使用@RequestParam绑定请求参数时：
     *  若"Content-Type"="application/x-www-form-urlencoded"，post get方法都可以支持 
     *	若"Content-Type"="application/json"，只支持 get方法
     */
    @ApiOperation(value="样例-删除",notes="id为目标对象",produces = MediaType.APPLICATION_JSON_VALUE)
    public Integer delete(@RequestParam(value = "id",required=false) String id,@RequestParam(value = "type",required=false) String type,HttpServletRequest request) {
    	LOGGER.info(request.getParameter("id"));
    	LOGGER.info("delete Sample: id={},type={}" , id,type );
		Sample entity = new Sample(id);
		return sampleBiz.delete(entity);
    }
    
    
    /**
     * 删除2（@RequestBody绑定参数）
     * @param entity
     * @return
     */
//    @RequestMapping(method={RequestMethod.POST}  ,value = "/dd")
    @PostMapping("dd")
    public Integer delete2(@RequestBody Sample entity) {
    	LOGGER.info("delete Sample: {} " , entity.toString() );
		return sampleBiz.delete(entity);
    }
}